<?php
class Tourism_delivery_address_model extends BZ_Model
{
    protected $table = 'tourism_delivery_address';

    /**
     * 根据UId获取
     * @param $uid
     * @return mixed
     */
    public function findDefaultAddress($uid)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('uid', $uid);
        $this->db->where('is_default', 2);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 更新所有地址的 变更为非默认
     * @param $postData
     * @return mixed
     */
    public function updateAddress($postData)
    {
       $data = array(
            'province_id'   => $postData['province_id'],
            'province_name' => $postData['province_name'],
            'city_id'       => $postData['city_id'],
            'city_name'     => $postData['city_name'],
            'district_id'   => $postData['district_id'],
            'district_name' => $postData['district_name'],
            'detailed'      => $postData['detailed'],
            'code'          => $postData['code'],
            'receiver_name' => $postData['receiver_name'],
            'tel'           => $postData['tel']
        );
        if (isset($postData['is_default'])) {
            $data['is_default'] = $postData['is_default'];
        }
        $this->db->where('address_id', $postData['address_id']);
        return $this->db->update($this->table,$data);
    }

    /**
     * 把默认地址全部改为不默认
     * @param $uid
     * @return mixed
     */
    public function setNonDefault($uid)
    {
        $this->db->where('uid', $uid);
        return $this->db->update($this->table, array('is_default' => 1));
    }

    /**
     * 设置默认地址
     * @param $addressId
     * @return mixed
     */
    public function setDefault($addressId)
    {
       $this->db->where('address_id', $addressId);
       return $this->db->update($this->table, array('is_default' => 2));
    }
}